package main

import (
	"bufio"
	"fmt"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/transform"
	"io"
	"io/ioutil"
	"net/http"
	"regexp"
)

func main0() {
	resp, err := http.Get("http://www.zhenai.com/zhenghun")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		fmt.Println("Error: status code", resp.StatusCode)
		return
	}

	// #1
	// <meta charset="gbk"> 如果网站编码格式为gbk，则要转成utf-8，现在获取的是utf-8格式
	//utf8Reader := transform.NewReader(resp.Body, simplifiedchinese.GBK.NewDecoder())

	// #2 先获取下页面的编码格式，再进行转换
	//e := determineEncoding(resp.Body)
	//utf8Reader := transform.NewReader(resp.Body, e.NewDecoder())
	bodyReader := bufio.NewReader(resp.Body)
	e := determineEncoding2(bodyReader)
	utf8Reader := transform.NewReader(bodyReader, e.NewDecoder())

	// httputil.DumpResponse() 会包含头部信息
	//all, err := ioutil.ReadAll(resp.Body)
	all, err := ioutil.ReadAll(utf8Reader) // <meta charset="utf-8">
	if err != nil {
		panic(err)
	}
	//fmt.Printf("%s\n", all)

	printCityList(all)
}

func determineEncoding(r io.Reader) encoding.Encoding {
	// bufio.NewReader会造成r的前1024字节读不回来了
	bytes, err := bufio.NewReader(r).Peek(1024) // charset.DetermineEncoding()函数里有判断，最多读1024
	if err != nil {
		panic(err)
	}
	e, _, _ := charset.DetermineEncoding(bytes, "")
	return e
}

func determineEncoding2(r *bufio.Reader) encoding.Encoding {
	bytes, err := r.Peek(1024)
	if err != nil {
		panic(err)
	}
	e, _, _ := charset.DetermineEncoding(bytes, "")
	return e
}

func printCityList(contents []byte) {
	// <a href="http://www.zhenai.com/zhenghun/xuhui" data-v-1573aa7c>徐汇</a>
	// 用正则表达式找出url和城市名
	re := regexp.MustCompile(`<a href="(http://www.zhenai.com/zhenghun/[0-9a-z]+)"[^>]*>([^<]+)</a>`)
	matches := re.FindAllSubmatch(contents, -1)
	//for _, m := range matches {
	//	for _, subMatch := range m {
	//		fmt.Printf("%s ", subMatch)
	//	}
	//	fmt.Println()
	//}
	for _, m := range matches {
		fmt.Printf("City: %s, URL: %s\n", m[2], m[1])
	}
	fmt.Printf("Matches found: %d\n", len(matches))
}
